import config from "./config";
import { excludeRoutes, defaultRoutes, getSiteMapList } from "./routes";

const sitemap = {
  path: "/sitemap.xml", // 生成的文件路径
  hostname: config.url, // 网址
  //cacheTime: 1000 * 60 * 60 * 24, // 1天 更新频率，只在 generate: false有用
  cacheTime: 1000,
  gzip: true, // 生成 .xml.gz 压缩的 sitemap
  generate: false, // 允许使用 nuxt generate 生成
  // 排除不要页面
  exclude: excludeRoutes, //  这里的路径相对 hostname,
  // xml默认的配置
  defaults: {
    changefreq: "always",
    lastmod: new Date(),
  },
  // 需要生成的xml数据, return 返回需要给出的xml数据
  routes: async () => {
    let routes = defaultRoutes;

    // 获取所有医院
    const _hospital = await getSiteMapList(1);
    if (_hospital && _hospital.length) {
      _hospital.map((item) =>
        routes.push({
          url: `/hospital/detail/${item}`,
          lastmod: new Date(),
          changefreq: "always",
        })
      );
    }

    // 获取所有医生
    const _doctor = await getSiteMapList(2);
    if (_doctor && _doctor.length) {
      _doctor.map((item) =>
        routes.push({
          url: `/doctor/detail/${item}`,
          lastmod: new Date(),
          changefreq: "always",
        })
      );
    }

    // 获取所有商品
    const _goods = await getSiteMapList(3);
    if (_goods && _goods.length) {
      _goods.map((item) =>
        routes.push({
          url: `/goods/detail/${item}`,
          lastmod: new Date(),
          changefreq: "always",
        })
      );
    }

    // 获取所有资讯
    const _news = await getSiteMapList(4);
    if (_news && _news.length) {
      _news.map((item) =>
        routes.push({
          url: `/news/detail/${item}`,
          lastmod: new Date(),
          changefreq: "always",
        })
      );
    }

    // 获取所有问答
    const _question = await getSiteMapList(5);
    if (_question && _question.length) {
      _question.map((item) =>
        routes.push({
          url: `/questions/${item}`,
          lastmod: new Date(),
          changefreq: "always",
        })
      );
    }

    // 获取所有医院资质
    const _hosp = await getSiteMapList(6);
    if (_hosp && _hosp.length) {
      _hosp.map((item) =>
        routes.push({
          url: `/inquire/hospital/${item}`,
          lastmod: new Date(),
          changefreq: "always",
        })
      );
    }

    // 获取所有医生资质
    const _doctorCer = await getSiteMapList(7);
    if (_doctorCer && _doctorCer.length) {
      _doctorCer.map((item) =>
        routes.push({
          url: `/inquire/doctor/${item}`,
          lastmod: new Date(),
          changefreq: "always",
        })
      );
    }

    // 获取所有材料资质
    const _material = await getSiteMapList(8);
    if (_material && _material.length) {
      _material.map((item) =>
        routes.push({
          url: `/inquire/material/${item}`,
          lastmod: new Date(),
          changefreq: "always",
        })
      );
    }

    // 获取所有案例
    const _case = await getSiteMapList(9);
    if (_case && _case.length) {
      _case.map((item) =>
        routes.push({
          url: `/inquire/case/detail/${item}`,
          lastmod: new Date(),
          changefreq: "always",
        })
      );
    }

    // 获取所有百科
    const _baike = await getSiteMapList(10);
    if (_baike && _baike.length) {
      _baike.map((item) =>
        routes.push({
          url: `/baike/${item}`,
          lastmod: new Date(),
          changefreq: "always",
        })
      );
    }

    return routes;
  },
};

export default sitemap;
